package com.hkbigdata.watermark;

import com.hkbigdata.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * @author liuanbo
 * @creat 2024-05-06-15:06
 * @see 2194550857@qq.com
 */
public class Flink10_Chapter07_OrderedWaterMark {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> waterSensorSingleOutputStreamOperator = env.socketTextStream("hadoop102", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0],
                                Long.valueOf(split[1]),
                                Integer.valueOf(split[2])
                        );
                    }
                });
        //watermark=数据的事件时间-数据的乱序程度
        //1.创建watermark策略
        WatermarkStrategy<WaterSensor> waterSensorWatermarkStrategy = WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                    @Override
                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                        //提取数据里面的时间戳作为watermark事件时间
                        return element.getTs() * 1000L;
                    }
                });

        SingleOutputStreamOperator<String> result = waterSensorSingleOutputStreamOperator.
                assignTimestampsAndWatermarks(waterSensorWatermarkStrategy)
                .keyBy(WaterSensor::getId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .allowedLateness(Time.seconds(2))
                .sideOutputLateData(new OutputTag<WaterSensor>("sideid") {
                })
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String key, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {

                        String message = "当前key:" + key + "窗口[" + context.window().getStart() / 1000L + "==" + context.window().getEnd() / 1000L + "]" + elements.spliterator().estimateSize() + "条数" + "watermark:" + context.currentWatermark();
                        out.collect(message);
                    }
                });


        /**
         * watermark =事件时间-乱序程度
         * 当watermark大于或者等于关窗时间，窗口关闭，否则如果小于关窗时间，那窗口开启
         *开了一个5秒钟的滚动窗口
         * 窗口关闭，又延迟2秒钟关闭 =》那真正关窗时间=watermark乱序+延迟时间,开始计算，但是并没有关窗
         * 那如果说关窗之后，后面到的数据，只能走侧输出流
         */


        result.print("主流");

        result.getSideOutput(new OutputTag<WaterSensor>("sideid") {
        }).print("侧输出流");

        env.execute();


    }
}
